import router from '../router/index'
import axios from 'axios'
import qs from 'qs'
// import { MessageBox } from 'element-ui';

// const cancel = false

// let BASE_URL = 'http://192.168.1.96:8000/'
let BASE_URL = 'http://218.75.49.82:7180/'
// var BASE_URL = 'http://daoting.co:9680/'
// var BASE_URL = 'http://192.168.2.113:8000/'
const http = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? '' : BASE_URL,
  // 跨域请求时是否需要使用凭证
  withCredentials: false,
  // 即将被发送的自定义请求头
  timeout: 10000
})

// 高德地图申请的 web服务   key
const AMapKey = '59ad928397c6e2c7d507b2d0089d16eb'

// 高德地图申请的 web端（JS Api）   key
const AMapKeyWebJs = '203b81d30397444b5c0e4f629f62ee58'

// 添加请求拦截器
http.interceptors.request.use(
  config => {
    config.headers.common.authorization = localStorage.getItem('token') || ''
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 路由拦截器
http.interceptors.response.use(
  response => {
    if (response.status === 200) {
      // if (response.headers.authorization) {
      //   localStorage.setItem('token', response.headers.authorization)
      // }
      const { code, message } = response.data
      if (code === 300 || code === 1001) {
        router.push('/login')
      }
      if (code !== 200) {
        console.log(message)
        // this.$Message.success('请选择要通知的专家')
        // this.$Message.error(message)
      }
    } else {
      router.push('/login')
    }
    // console.log(response)
    return response
  },
  error => {
    // console.log('登录超时');
    if (error) {
    }
    const response = {
      data: {
        code: -1,
        message: '登录超时'
      }
    }
    return response
  }
)

// 发送post请求
export function post (url, params) {
  return new Promise((resolve, reject) => {
    http.defaults.headers.post['Content-Type'] = 'application/json'
    http
      .post(url, params)
      .then(
        response => {
          resolve(response.data)
        },
        err => {
          reject(err)
        }
      )
      .catch(error => {
        reject(error)
      })
  })
}

// 发送get请求
export function get (url, param) {
  return new Promise((resolve, reject) => {
    http
      .get(url, {
        params: param
      })
      .then(
        response => {
          resolve(response.data)
        },
        err => {
          reject(err)
          console.log(err)
        }
      )
      .catch(error => {
        console.log(error)
        reject(error)
      })
  })
}

// 发送post请求（表单格式）
export function formPost (url, params) {
  return new Promise((resolve, reject) => {
    http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
    http
      .post(url, qs.stringify(params))
      .then(
        response => {
          resolve(response.data)
        },
        err => {
          console.log(err)
          reject(err)
        }
      )
      .catch(error => {
        console.log(error)
        reject(error)
      })
  })
}

export default {
  get,
  post,
  formPost,
  BASE_URL
}
// export { BASE_URL,imgUrl };
export { BASE_URL, AMapKey, AMapKeyWebJs }
